package com.aaa.book_mall.controller;

import com.aaa.book_mall.dao.*;
import com.aaa.book_mall.dao.impl.*;
import com.aaa.book_mall.entity.*;
import com.aaa.book_mall.util.LayTab;
import com.aaa.book_mall.util.Result;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 入库信息
 * fileName:RequestServlet
 * description:
 * author:付亚星
 * createTime:2021/1/18 11:56
 * version:1.0.0
 */

@RestController
@RequestMapping("/request")
public class RequestController {

    private BookDao bookDao = new BookDaoImpl();
    private AuditDao auditDao = new AuditDaoImpl();
    private DetailDao detailDao = new DetailDaoImpl();
    private RequestDao requestDao = new RequestDaoImpl();
    private RecordDao recordDao = new RecordDaoImpl();
    private ProlnDao prolnDao = new ProlnDaoImpl();

    /**
     * 向t_request、t_detail表中添加数据
     */
    @RequestMapping("/insert")
    public Map insert(@RequestBody ArrayList<Book> bookdata,Request outs, HttpServletRequest request) {
        System.out.println(bookdata);
        System.out.println("---------------------------");
        //      通过session获取登录人的信息，用以生成outnumber
        HttpSession session = request.getSession();
        Staff staff =(Staff) session.getAttribute("staff");
        //        获取session中的员工编号
       // Object staffid = session.getAttribute("staffid");
        Integer staffId = staff.getStaffId();
        //        获取session中的员工姓名
        String staffname = staff.getStaffName();
        //int staffId = Integer.parseInt(staffid + "");
        //System.out.println("提交申请staffId" + staffId);
        //        获取现在的时间，和用户便函共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate = staffId + format.replace("-", "").replace(":", "");
        System.out.println("outnumber" + stringDate);
//        计算该次订单共又多少本书，共多少钱
        int bookcount = 0;
        double bookprice = 0;
        //      循环遍历前台输入的数据
        if (bookdata.size() > 0) {
            for (Book bookdatum : bookdata) {
                //入库详情表
                Detail detail = new Detail(null, stringDate, bookdatum.getBookid(), bookdatum.getCount());
//               向detail表中加入数据
                int i = detailDao.insertDetail(detail);
                Proln proln = new Proln(null,stringDate,bookdatum.getBookid(),bookdatum.getCount());
                int i1 = prolnDao.insertProln(proln);
                System.out.println(i+":"+i1);
                System.out.println("...................................");
//
                bookcount += bookdatum.getCount();
                bookprice += (bookdatum.getCount() * bookdatum.getPrice());
                System.out.println(bookcount + ":" + bookprice);
            }
        }
        Request out = new Request(null, stringDate,staffId, staffname, bookcount, bookprice, date, "0");
//       向申请采购表中写数据
        int i = requestDao.insertRequest(out);
        //向入库记录表添加内容
       // Record record = new Record(null,stringDate,109,"小蓝",bookcount,bookprice,date);
        //int i1 = recordDao.insertRecord(record);
        HashMap<Object, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", "1");
            map.put("msg", "添加成功");
        } else {
            map.put("code", "0");
            map.put("msg", "添加失败");
        }
        return map;
    }

    /**
     * 审核通过时得操作
     */
    @RequestMapping("/agree")
    public Map agree(@RequestBody Request outs,HttpServletRequest request){
        String status="1";
        //        1、更改t_out表中的状态
        int statusV = requestDao.updateStatus(outs.getNumber(),status);
        System.out.println(status);
        /*       2、更改t_book对应书本的数量
         *             先根据outnumber到t_outdetail表中查询申请的每一本书
         *             根据查询出来的书的量和书的编号到t_book表中操作
         * */
        List<Detail> outDetails = detailDao.queryDetail(outs.getNumber());//查询订单详情
        if(outDetails!=null && outDetails.size()>0){
            for (Detail outDetail : outDetails) {
                int bookcount = bookDao.selectCountByBookid(outDetail.getBookid());//查询t_book中的书本数量
               // double price = bookDao.selectPriceByBookid(outDetail.getBookid());//查询t_book中的书本进价
                bookcount=bookcount+outDetail.getBookcount();//将原有数量加上申请的数量
                bookDao.updateCountByBookid(bookcount,outDetail.getBookid());//更新t_book中的count
            }
        }
        //        3、写入到t_audit表
        //      通过session获取登录人的信息，用以生成outnumber
        HttpSession session = request.getSession();
        Staff staff =(Staff) session.getAttribute("staff");
////      获取session中的员工编号
        //Object staffid = session.getAttribute("staffid");
        Integer staffId = staff.getStaffId();
        //        获取session中的员工姓名
        String staffname = staff.getStaffName();
//        System.out.println("审核人员的staffId"+staffId);
//        获取现在的时间，和用户便函共同组成outnumber
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        String format = simpleDateFormat.format(date);
        String stringDate =staffId+format.replace("-", "").replace(":","");

        Audit audit = new Audit(null, stringDate, staffId, staffname, outs.getNumber(), "同意", "同意", date);
        int i = auditDao.insertAudit(audit);
        //向入库记录表添加内容
       // Record record = new Record(null,stringDate,101,"小楚",outs.getBookcount(),outs.getBookprice(),date);
        //int i1 = recordDao.insertRecord(record);
        //向入库记录表添加内容
        Record record = new Record(null,outs.getNumber(),staffId,staffname,outs.getBookcount(),outs.getBookprice(),date);
        int i1 = recordDao.insertRecord(record);
        HashMap<Object, Object> map = new HashMap<>();
        if(i>0){
            map.put("code","1");
            map.put("msg","操作成功");
        }else{
            map.put("code","0");
            map.put("msg","操作失败");
        }
        return map;
    }


    /**
     * 查询所有没有审核的数据
     */
    @RequestMapping("/showNoAudit")
    public LayTab noAudit(Request request){
        List<Map<String, Object>> maps = requestDao.queryNoAuditInfo(request);
        System.out.println(maps);
//        查询没有审核数据的总数
        int countInfo = requestDao.queryCountInfo();
        System.out.println(countInfo);
        LayTab layTab = new LayTab(countInfo, maps);
        System.out.println(layTab);
        return layTab;
    }

    /**
     * 查询所有t_request的信息
     */
    @RequestMapping("/getAllRequest")
    public LayTab getAllRequest(Request request){
        System.out.println(request);
//        查询出来目标数据
        List<Map<String, Object>> allOutInfo = requestDao.getAllRequest(request);
//        查询出来所有的总条数
        int count = requestDao.showByCountInfo();
        LayTab layTab = new LayTab(count, allOutInfo);
        return layTab;
    }

    /**
     * 查询被驳回的数据信息
     */
    @RequestMapping("/showNoAuditInfo")
    public LayTab showNoAuditInfo(Request request){
        System.out.println("查询驳回信息");
        //        查询出来目标数据
        List<Map<String, Object>> list = requestDao.showNoAuditInfo(request);
        int count = requestDao.showCountInfo();
        //        查询出来所有的总条数
        LayTab layTab = new LayTab(count, list);
        return layTab;
    }

    /**
     * 删除被驳回信息
     */
    @RequestMapping("/deleteRequest")
    public int deleteRequest(String status){
        System.out.println("开始删除驳回信息。。。");
        //获取status
        int i = requestDao.deleteRequest(status);
        System.out.println(i+"删除成功！");
        return i;
    }

    /**
     * 查询入库总金额
     */
    @RequestMapping("/selectRequest")
    public List<Map<String, Object>> selectRequest(){
        System.out.println("查询入库总金额。。");
        List<Map<String, Object>> maps = requestDao.selectRequest();
        System.out.println(maps);
        return maps;

    }
}





